package com.lvnnet.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ReadHttpObjectServlet
 */
@WebServlet("/readHttpObjectServlet.do")
public class ReadHttpObjectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReadHttpObjectServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**
		 * 1.获得客户机信息
		 */
		String requestUrl = request.getRequestURL().toString();//得到请求的URL地址
		String requestUri = request.getRequestURI();//得到请求的资源
		String queryString = request.getQueryString();//得到请求的URL地址中附带的参数
		String remoteAddr = request.getRemoteAddr();//得到来访者的IP地址
		String remoteHost = request.getRemoteHost();
		int remotePort = request.getRemotePort();
		String remoteUser = request.getRemoteUser();
		String method = request.getMethod();//得到请求URL地址时使用的方法
		String pathInfo = request.getPathInfo();
		String localAddr = request.getLocalAddr();//获取WEB服务器的IP地址
		String localName = request.getLocalName();//获取WEB服务器的主机名
		response.setCharacterEncoding("UTF-8");//设置将字符以"UTF-8"编码输出到客户端浏览器
		//通过设置响应头控制浏览器以UTF-8的编码显示数据，如果不加这句话，那么浏览器显示的将是乱码
		response.setHeader("content-type", "text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write("获取到的客户机信息如下：");
		out.write("<hr/>");
		out.write("请求的URL地址："+requestUrl);
		out.write("<br/>");
		out.write("请求的资源："+requestUri);
		out.write("<br/>");
		out.write("请求的URL地址中附带的参数："+queryString);
		out.write("<br/>");
		out.write("来访者的IP地址："+remoteAddr);
		out.write("<br/>");
		out.write("来访者的主机名："+remoteHost);
		out.write("<br/>");
		out.write("使用的端口号："+remotePort);
		out.write("<br/>");
		out.write("remoteUser："+remoteUser);
		out.write("<br/>");
		out.write("请求使用的方法："+method);
		out.write("<br/>");
		out.write("pathInfo："+pathInfo);
		out.write("<br/>");
		out.write("localAddr："+localAddr);
		out.write("<br/>");
		out.write("localName："+localName);
		out.write("<br/>");
		out.write("===========头部信息=============");
		out.write("<br/>");
		Map<String, String> headMap = getHeadersInfo(request);
		if(null != headMap){
			Iterator<Entry<String, String>> ite = headMap.entrySet().iterator();
			while(ite.hasNext()){
				Entry<String, String> ent = ite.next();
				out.write(ent.getKey() + "："+ ent.getValue());
				out.write("<br/>");
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	/**
	 * 获取请求头
	 * @param request
	 * @return
	 */
	private Map<String, String> getHeadersInfo(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		Enumeration<String> headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String key = (String) headerNames.nextElement();
			String value = request.getHeader(key);
			map.put(key, value);
		}
		return map;
	}
}
